#include "stdafx.h"
#include "XamlTextReader.h"

#include <xmllite.h>

#include "XamlNode.h"
#include "XamlParserContext.h"
#include "XamlScanner.h"
#include "IPALDebuggingServices.h"
#include "IPlatformServices.h"
#include "CWindowsServices.h"
#include "CXMLParser.h"
#include "XamlPullParser.h"

#include "EncodedPtr.h"

extern EncodedPtr<IPlatformServices> gps;


XamlTextReader::XamlTextReader(const xshared_ptr<XamlSchemaContext> & spXamlSchemaContext, const xshared_ptr<XamlPullParser> spXamlPullParser) :
	m_spXamlPullParser(spXamlPullParser),
	m_spXamlSchemaContext(spXamlSchemaContext)

{

}

HRESULT XamlTextReader::Create(xshared_ptr<XamlSchemaContext> spXamlSchemaContext, xshared_ptr<XamlTextReaderSettings> spXamlTextReaderSettings, XUINT32 cSource, XUINT8 * pSource, xshared_ptr<XamlTextReader> & spXamlTextReader)
{

	HRESULT hr = S_OK;
	IPALReader * pReader = NULL;
	xshared_ptr<XamlParserContext> spParserContext;
	xshared_ptr<XamlScanner> scanner;
	xshared_ptr<XamlPullParser> parser;

	XASSERT_SUCCESSED(gps->ReaderCreate(&pReader));

	XASSERT_SUCCESSED(CXMLParser::StripLeadingWhitespace(&cSource,&pSource));

	XASSERT_SUCCESSED(pReader->SetInput(cSource,pSource));

	XASSERT_SUCCESSED(XamlParserContext::Create(spXamlSchemaContext, spParserContext));

	XASSERT_SUCCESSED(scanner.init(new XamlScanner(spParserContext, pReader, spXamlTextReaderSettings)));

	XASSERT_SUCCESSED(scanner->Init());

	XASSERT_SUCCESSED(parser.init(new XamlPullParser(spParserContext, scanner)));

	XASSERT_SUCCESSED(spXamlTextReader.init(new XamlTextReader(spXamlSchemaContext, parser)));

	return hr;

}

XamlTextReader::~XamlTextReader()
{

	//NOTIMPL

}

HRESULT XamlTextReader::Read()
{
	return this->m_spXamlPullParser->Parse(this->m_currentXamlNode);
}

const XamlNode & XamlTextReader::CurrentNode()
{
	return this->m_currentXamlNode;
}

HRESULT XamlTextReader::GetSchemaContext(xshared_ptr<XamlSchemaContext> & outSchemaContext)
{
	return this->m_spXamlSchemaContext.lock(outSchemaContext);
}
